# -*- coding: utf-8 -*-
import datetime
import pickle
import pandas as pd
import threading
import os
from pao.data import process_db
from gov.data_process.data import DataProcess
from gov.data_process.dlh_screen_display.sim_basic_data import Gener_Sim_Data
from apscheduler.schedulers.blocking import BlockingScheduler

class InsertData(DataProcess):
    # 获取最大时间的mongodb查询语句
    pipeline = [{u"$group": {u"_id": {}, u"MAX(date)": {u"$max": u"$date"}}}, {
        u"$project": {u"_id": 0, u"MAX(date)": u"$MAX(date)"}}]

    def __init__(self, db_addr, db_port, dlh_equip_num, cal_A):
        DataProcess.__init__(self, db_addr, db_port)
        self.dlh_equip_num = dlh_equip_num
        self.cal_A = cal_A

    def get_maxdate(self, collection_name):
        # 获取表中最大日期
        res = ''

        def process_func(db):
            nonlocal res
            collection = db[collection_name]
            cursor = collection.aggregate(self.pipeline, allowDiskUse=True)
            for doc in cursor:
                res = doc
        process_db(self.db_addr, self.db_port, 'GovNetThing', process_func)
        res = res["MAX(date)"]
        return res

    def buchong_equip(self):
        # 补充analy_equip中的数据
        maxdate = self.get_maxdate('analy_equip')
        date_dq = datetime.datetime.now()
        while date_dq > maxdate:
            dates = pd.date_range(maxdate, date_dq, freq='H')
            for one_hour in dates:
                Sim_data = Gener_Sim_Data(
                    self.db_addr, self.db_port, date=one_hour)
                Sim_data.insert_analy_equip(self.cal_A, self.dlh_equip_num)
            maxdate = self.get_maxdate('analy_equip')
            date_dq = datetime.datetime.now()

    def buchong_ydb(self):
        # 补充ydb中的数据
        maxdate = self.get_maxdate('ydb')
        date_dq = datetime.datetime.now()
        while date_dq > maxdate:
            dates = pd.date_range(maxdate, date_dq, freq='H')
            for one_hour in dates:
                Sim_data = Gener_Sim_Data(
                    self.db_addr, self.db_port, date=one_hour)
                Sim_data.insert_ydb(self.cal_A)
            maxdate = self.get_maxdate('ydb')
            date_dq = datetime.datetime.now()

    def buchong_TJ_ydb(self):
        # 补充TJ_ydb的数据
        maxdate = self.get_maxdate('TJ_ydb')
        date_dq = datetime.datetime.now()
        if maxdate.strftime("%Y-%m-%d") < date_dq.strftime("%Y-%m-%d"):
            dates = pd.date_range(maxdate, date_dq, freq='D')
            if len(dates) > 2:
                dates2 = dates[2:]
                for i_day in dates2:
                    Sim_data = Gener_Sim_Data(
                        self.db_addr, self.db_port, date=i_day)
                    Sim_data.insert_TJ_ydb()

    def tep_ydb(self):
        # 建立ydb实时数据插入的实例化程序
        date_now = datetime.datetime.now()
        Sim_data = Gener_Sim_Data(self.db_addr, self.db_port, date=date_now)
        Sim_data.insert_ydb(self.cal_A)

    def tep_equip(self):
        # 建立analy_equip实时数据插入的实例化程序
        date_now = datetime.datetime.now()
        Sim_data = Gener_Sim_Data(self.db_addr, self.db_port, date=date_now)
        Sim_data.insert_analy_equip(self.cal_A, self.dlh_equip_num)

    def tep_TJ_ydb(self):
        # 建立TJ_ydb实时数据插入的实例化程序
        date_now = datetime.datetime.now()
        Sim_data = Gener_Sim_Data(self.db_addr, self.db_port, date=date_now)
        Sim_data.insert_TJ_ydb()

    def gener_data_ydb(self):
        # 建立ybd的实时数据插入的调度程序
        scheduler = BlockingScheduler()
        scheduler.add_job(self.tep_ydb, 'cron', hour='0-23', minute='0')
        scheduler.start()

    def gener_data_equip(self):
        # 建立analy_equip的实时数据插入的调度程序
        scheduler = BlockingScheduler()
        scheduler.add_job(self.tep_equip, 'cron', hour='0-23', minute='0')
        scheduler.start()

    def gener_data_TJydb(self):
        # 建立TJ_ybd的实时数据插入的调度程序
        scheduler = BlockingScheduler()
        scheduler.add_job(self.tep_TJ_ydb, 'cron',
                          day_of_week='*', hour=1, minute=0, second=0)
        scheduler.start()

    def gener_2mins(self):
        # 2分钟生成一次的实时数据的调度程序
        date_now = datetime.datetime.now()
        Sim_data_init = Gener_Sim_Data(
            date=date_now, db_addr=self.db_addr, db_port=self.db_port)
        Sim_data_init.cal_data_2mins()
        scheduler = BlockingScheduler()
        scheduler.add_job(Sim_data_init.insert_data_2mins,
                          'cron', hour='0-23', minute='*/2')
        scheduler.start()

    def gener_ycdata(self):
        # 异常数据的实时生成
        date_now = datetime.datetime.now()
        Sim_data_init = Gener_Sim_Data(
            date=date_now, db_addr=self.db_addr, db_port=self.db_port)
        scheduler = BlockingScheduler()
        scheduler.add_job(Sim_data_init.insert_yichangdata,
                          'interval', seconds=50)
        scheduler.start()

